package com.ygqh.baby.service.impl;

import java.util.List;
import java.util.Map;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgAttribute;
import com.ygqh.baby.mapper.YgAttributeMapper;
import com.ygqh.baby.service.YgAttributeService;

import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgAttributeExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class YgAttributeServiceImpl implements YgAttributeService {

	@Autowired
	private YgAttributeMapper ygAttributeMapper;

	@Override
	public List<YgAttribute> find() {
		YgAttributeExample example = new YgAttributeExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAttributeMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgAttributeExample example = new YgAttributeExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygAttributeMapper.countByExample(example);
	}

	@Override
	public YgAttribute findById(Long id) {
		return ygAttributeMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgAttribute ygAttribute) {
		return ygAttributeMapper.insertSelective(ygAttribute);
	}

	@Override
	public int update(YgAttribute ygAttribute) {
		return ygAttributeMapper.updateByPrimaryKeySelective(ygAttribute);
	}

	@Override
	public ResultSet<YgAttribute> search(QueryInfo queryInfo, String q) {
		List<YgAttribute> list = ygAttributeMapper.selectSuper(queryInfo, q);
		int count = ygAttributeMapper.countSuper(q);
		ResultSet<YgAttribute> resultSet = new ResultSet<YgAttribute>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgAttributeExample example = new YgAttributeExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgAttribute record = new YgAttribute();
		record.setStatus(DataStatus.Delete);

		return ygAttributeMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgAttribute ygAttribute) {
		int r = 0;
		if (ygAttribute.getId() != null) {
			r = this.update(ygAttribute);
		} else {
			r = this.save(ygAttribute);
		}
		return r;
	}

	@Override
	public boolean validateAttributeName(Long parentId, String attributeName, Long id) {
		YgAttributeExample example = new YgAttributeExample();
		YgAttributeExample.Criteria criteria = example.createCriteria();
		criteria.andAttributeNameEqualTo(attributeName).andParentIdEqualTo(parentId).andStatusEqualTo(DataStatus.Valid.name());
		if (id != null) {
			criteria.andIdNotEqualTo(id);
		}
		return ygAttributeMapper.countByExample(example) == 0;
	}

	@Override
	public List<YgAttribute> findByParentId(Long parentId) {

		return ygAttributeMapper.selectAttributeByParentId(parentId);
	}

	
}
